# -*- coding: utf-8 -*-
"""
# 使用了 [pandas==2.0.3]，遵循其 [BSD 3-Clause License] 许可证，原始代码来源：[https://pandas.pydata.org]
# 使用了 [scipy==1.10.1]，遵循其 [BSD License] 许可证，原始代码来源：[https://scipy.org/]
"""
import sys
from pathlib import Path
import pandas as pd
from scipy import stats

class PARA_CONFIG:
    CURRENT_DIR=Path(__file__).parent.resolve()
    KMEANS_PATH=CURRENT_DIR.parent.parent.resolve()/"03_kmeans"/"src"/"result"/"5_Kmeans_SK_01_kmeans_20251031_150848.xlsx"
    P_THRES: float = 0.001
    FUNC_PATH=CURRENT_DIR.parent.parent.resolve()/"about_file"
sys.path.append(str(PARA_CONFIG.FUNC_PATH))
import f_basic

@f_basic.Timer
def f_oneway_check(fp_k: str, p_threshold: float =PARA_CONFIG.P_THRES) -> str:
    df=pd.read_excel(fp_k)
    df.columns=["T#"+str(col) for col in df.columns]
    df=df.rename(columns={"T#label":"label"})
    results=[]
    label_groups=df.groupby('label')
    for col in df.columns.drop('label'):
        groups=[group[col].values for _,group in label_groups if not group.empty]
        if len(groups)<2:continue#skip.
        f_val,p_val=stats.f_oneway(*groups)
        if p_val<p_threshold:
            results.append((col,f_val,p_val))
    return(f_basic.save_dataframe(pd.DataFrame(results,columns=["Topic","F_value","p_value"]),prefix="6_F_check_SK"))

@f_basic.Timer 
def main_stream(fp_kmeans):
    """F check start."""
    f_oneway_check(fp_kmeans)

if __name__=="__main__":
    main_stream(PARA_CONFIG.KMEANS_PATH)